package com.itcast.bean.product;

import java.io.Serializable;
import java.util.HashSet;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;

import org.hibernate.annotations.Type;

@Entity
public class ProductType implements Serializable{
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	/*类别id*/
	private Integer typeid;
	/*类别名称*/
	private String name;
	/*备注，用于google搜索页面描述*/
	private String note;
	/*是否可见*/
	private boolean visible=true;
	/*子类别*/
	private Set<ProductType> childtypes=new HashSet<ProductType>();
	/*所属父类别*/
	private ProductType parent;
	@Override
	public String toString() {
		return "ProductType [typeid=" + typeid + ", name=" + name + ", note="
				+ note + ", visible=" + visible + "]";
	}


	@Id @GeneratedValue(strategy=GenerationType.AUTO)
	public Integer getTypeid() {
		return typeid;
	}
	
	
	public void setTypeid(Integer typeid) {
		this.typeid = typeid;
	}

	@Column(length=36,nullable=false)
	public String getName() {
		return name;
	}


	public void setName(String name) {
		this.name = name;
	}

	@Column(length=200)
	public String getNote() {
		return note;
	}


	public void setNote(String note) {
		this.note = note;
	}

	@Column(nullable=false,columnDefinition="TINYINT(1)")
	@Type(type="org.hibernate.type.NumericBooleanType")
	public boolean getVisible() {
		return visible;
	}


	public void setVisible(boolean visible) {
		this.visible = visible;
	}

	@OneToMany(cascade={CascadeType.REFRESH,CascadeType.REMOVE},mappedBy="parent")
	public Set<ProductType> getChildtypes() {
		return childtypes;
	}


	public void setChildtypes(Set<ProductType> childtypes) {
		this.childtypes = childtypes;
	}

	@ManyToOne(cascade=CascadeType.REFRESH,optional=true)//optional可以不赋值，默认值是optional=true;
	@JoinColumn(name="parentid")
	public ProductType getParent() {
		return parent;
	}


	public void setParent(ProductType parent) {
		this.parent = parent;
	}


	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((typeid == null) ? 0 : typeid.hashCode());
		return result;
	}


	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		ProductType other = (ProductType) obj;
		if (typeid == null) {
			if (other.typeid != null)
				return false;
		} else if (!typeid.equals(other.typeid))
			return false;
		return true;
	}
	
	
}
